package com.mrdeer.iot.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mrdeer.common.entity.beans.PageEntity;
import com.mrdeer.common.exception.BusinessException;
import com.mrdeer.common.exception.NotFoundException;
import com.mrdeer.iot.dto.ShopEntityDTO;
import com.mrdeer.iot.entity.ShopEntity;
import com.mrdeer.iot.mapper.ShopMapper;
import com.mrdeer.iot.service.ShopService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @Author: xie jianchu
 */
@Slf4j
@Service
@Transactional
public class ShopServiceImpl extends ServiceImpl<ShopMapper, ShopEntity> implements ShopService {

    @Resource
    private ShopMapper shopMapper;

    @Override
    public ShopEntity selectByName(String name) {
        LambdaQueryWrapper<ShopEntity> queryWrapper = Wrappers.<ShopEntity>query().lambda();
        queryWrapper.eq(ShopEntity::getShopName, name);
        ShopEntity shopEntity = shopMapper.selectOne(queryWrapper);
        if (null == shopEntity) {
            throw new NotFoundException("The Shop does not exist");
        }
        return shopEntity;
    }

    @Override
    public ShopEntity add(ShopEntity shopEntity) {

        try {
            selectByName(shopEntity.getShopName());
            throw new BusinessException("The shop already exists");
        } catch (NotFoundException e) {
            if (shopMapper.insert(shopEntity) > 0) {
                return shopMapper.selectById(shopEntity.getId());
            }
            throw new BusinessException("The shop add failed");
        }
    }

    @Override
    public boolean delete(String id) {
        ShopEntity shop = getById(id);
        if (null == shop) {
            throw new BusinessException("The shop does not exist");
        }
        return shopMapper.deleteById(id) > 0;
    }

    @Override
    public ShopEntity update(ShopEntity shopEntity) {
        if (shopMapper.updateById(shopEntity) > 0) {
            ShopEntity shop = shopMapper.selectById(shopEntity.getId());
            return shop;
        }
        throw new BusinessException("update shop failed");
    }

    @Override
    public ShopEntity getById(String id) {
        return shopMapper.selectById(id);
    }

    @Override
    public Page<ShopEntity> list(ShopEntityDTO dto) {
        if (null == dto.getPageEntity()) {
            dto.setPageEntity(new PageEntity());
        }
        return shopMapper.selectPage(dto.getPageEntity().convert(), fuzzyQuery(dto));
    }

    @Override
    public LambdaQueryWrapper<ShopEntity> fuzzyQuery(ShopEntityDTO dto) {
        LambdaQueryWrapper<ShopEntity> queryWrapper = Wrappers.<ShopEntity>query().lambda();
        if (null != dto) {
            if (StrUtil.isNotBlank(dto.getId())) {
                queryWrapper.eq(ShopEntity::getAddress, dto.getAddress());
            }
        }
        return queryWrapper;
    }
}
